package com.itheima.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.itheima.excption.BusinessException;
import com.itheima.excption.MyException;
import com.itheima.pojo.*;
import com.itheima.vo.ErrorResult;
import com.itheima.vo.PageResultVo;
import com.itheima.vo.Result;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleService {

    @DubboReference
    private UmsRoleApi umsRoleApi;

    @DubboReference
    private UmsRoleMenuRelationApi umsRoleMenuRelationApi;

    @DubboReference
    private UmsRoleResourceRelationApi umsRoleResourceRelationApi;

    @DubboReference
    private UmsMenuApi umsMenuApi;

    @DubboReference
    private UmsResourceApi umsResourceApi;

    @DubboReference
    private UmsAdminRoleRelationApi umsAdminRoleRelationApi;


    /**
     * 添加角色
     */
    public void create(UmsRole umsRole) {

        boolean flag = umsRoleApi.save(umsRole);
        if (flag){
            throw new MyException("该角色名已经存在,换个名字试试吧!");
        }
    }

    /**
     * 修改角色
     */
    public void update(Integer id, UmsRole umsRole) {

        boolean flag = umsRoleApi.update(id, umsRole);

        if (flag){
            throw new MyException("该角色名已经存在,换个名字试试吧!");
        }
    }

    /**
     * 修改角色状态
     */
    public void updateStatus(Integer id, Integer status) {
        umsRoleApi.updateStatus(id,status);
    }

    /**
     * 给角色分配菜单
     */
    public void allocMenu(ArrayList<Integer> menuIds, Integer roleId) {
        umsRoleMenuRelationApi.allocMenu(menuIds,roleId);
    }

    /**
     * 给角色分配资源
     */
    public void allocResource(Integer roleId, ArrayList<Integer> resourceIds) {
        umsRoleResourceRelationApi.allocResource(roleId,resourceIds);
    }

    /**
     * 批量删除角色
     */
    public void delete(ArrayList<Integer> ids) {

        Integer flag = umsAdminRoleRelationApi.findByRids(ids);

        if (flag == 2){
            throw new MyException("此用户为超级管理员,不可删除!");
        }

        if (flag ==1 ){
            throw new MyException("该角色下有用户,不可删除!");
        }

        umsRoleApi.delete(ids);
    }

    /**
     * 获取角色相关菜单
     */
    public List<UmsMenu> listMenu(Integer roleId) {
        //获取RoleMenuRelation集合
        List<UmsRoleMenuRelation> rmrList = umsRoleMenuRelationApi.listRmr(roleId);

        //根据RoleMenuRelation集合获取menu的id集合
        List<Long> menuIds = CollUtil.getFieldValues(rmrList, "menuId", Long.class);

        List<UmsMenu> menuList = new ArrayList<>();

        if (ObjectUtils.isNotEmpty(menuIds)){
            //根据id集合去menu表查询menulist
            menuList = umsMenuApi.findByIds(menuIds);
            return menuList;
        }

        return menuList;
    }

    /**
     * 获取资源相关资源列表
     */
    public List<UmsResource> listResource(Integer roleId) {
        List<UmsRoleResourceRelation> rrrList = umsRoleResourceRelationApi.listRrr(roleId);
        List<Long> resourceIds = CollUtil.getFieldValues(rrrList,"resourceId",Long.class);

        List<UmsResource> resourceList = new ArrayList<>();

        if (ObjectUtils.isNotEmpty(resourceIds)){
            resourceList = umsResourceApi.findByIds(resourceIds);
            return resourceList;
        }
        return resourceList;
    }

    /**
     * 分页查询  +  模糊查询
     */
    public PageResultVo<UmsRole> list(Integer pageNum, Integer pageSize, String keyword) {

        IPage<UmsRole> page = umsRoleApi.list(pageNum,pageSize,keyword);
        PageResultVo<UmsRole> pageResultVo = new PageResultVo<>();
        pageResultVo.setList(page.getRecords());
        pageResultVo.setPageNum((int) page.getCurrent());
        pageResultVo.setPageSize((int) page.getSize());
        pageResultVo.setTotal((int) page.getTotal());
        pageResultVo.setTotalPages((int) page.getPages());

        return pageResultVo;

    }

    /**
     * 查询所有
     */
    public List<UmsRole> listAll() {
        return umsRoleApi.listAll();
    }
}
